package de.tuberlin.dima.dkey.nerdle.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jdesktop.application.SingleFrameApplication;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import de.tuberlin.dima.dkey.client.Client;
import de.tuberlin.dima.dkey.client.GTree;
import de.tuberlin.dima.dkey.nerdle.helper.QueryHelper;
import de.tuberlin.dima.dkey.util.JsonTreeHelper;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * 
 */
public class NerdleGUI extends SingleFrameApplication {
	private JPanel topPanel;
	private JTextArea taResult;
	private JScrollPane jScrollPane1;
	private JLabel lblQT;
	private JTextField tfArg3;
	private JLabel lblObjects;
	private JTextField tfArg2;
	private JLabel lblArgs;
	private JTextField tfArg1;
	private JLabel lblPredicate;
	private JTextField tfPredicate;
	private JButton btnQuery;
	private JLabel lblResult;
	QueryHelper helper = new QueryHelper();

	@Override
	protected void startup() {
		{
			getMainFrame().setSize(1086, 588);
		}
		topPanel = new JPanel();
		topPanel.setPreferredSize(new java.awt.Dimension(500, 300));
		topPanel.setLayout(null);
		{
			jScrollPane1 = new JScrollPane();
			topPanel.add(jScrollPane1);
			jScrollPane1.setBounds(0, 157, 1071, 396);
			{
				taResult = new JTextArea();
				jScrollPane1.setViewportView(taResult);
				taResult.setBounds(0, 157, 896, 310);
				taResult.setName("taResult");
			}
		}
		{
			lblResult = new JLabel();
			topPanel.add(lblResult);
			lblResult.setBounds(0, 133, 94, 18);
			lblResult.setName("lblResult");
		}
		{
			btnQuery = new JButton();
			topPanel.add(btnQuery);
			btnQuery.setBounds(6, 31, 126, 30);
			btnQuery.setName("btnQuery");
			btnQuery.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					btnQueryActionPerformed(evt);
				}
			});
		}
		{
			tfPredicate = new JTextField();
			topPanel.add(tfPredicate);
			tfPredicate.setBounds(144, 32, 121, 28);
			tfPredicate.setName("tfPredicate");
		}
		{
			lblPredicate = new JLabel();
			topPanel.add(lblPredicate);
			lblPredicate.setBounds(168, 12, 67, 18);
			lblPredicate.setName("lblPredicate");
		}
		{
			tfArg1 = new JTextField();
			topPanel.add(tfArg1);
			tfArg1.setBounds(283, 32, 249, 28);
			tfArg1.setName("tfArg1");
		}
		{
			lblArgs = new JLabel();
			topPanel.add(lblArgs);
			lblArgs.setBounds(369, 12, 86, 18);
			lblArgs.setName("lblArgs");
		}
		{
			tfArg2 = new JTextField();
			topPanel.add(tfArg2);
			tfArg2.setBounds(548, 32, 249, 28);
			tfArg2.setName("tfArg2");
		}
		{
			lblObjects = new JLabel();
			topPanel.add(lblObjects);
			lblObjects.setBounds(638, 12, 61, 18);
			lblObjects.setName("lblObjects");
		}
		{
			tfArg3 = new JTextField();
			topPanel.add(tfArg3);
			tfArg3.setBounds(548, 72, 249, 28);
			tfArg3.setName("tfArg3");
		}
		{
			lblQT = new JLabel();
			topPanel.add(lblQT);
			lblQT.setBounds(0, 100, 47, 18);
			lblQT.setName("lblQT");
		}
		show(topPanel);
	}

	public static void main(String[] args) {
		launch(NerdleGUI.class, args);
	}

	private String makeDisplayString(String response, int questionType) {

		String answer = "";

		String obj = helper.getRole(response, "obj");
		String subj = helper.getRole(response, "sbj");

		String pred = helper.getPredicate(response, false);
		String predLemma = helper.getPredicate(response, true);

		/*
		 * display object first
		 */
		List<String> args = helper.getQueryArgs(response);

		if (questionType == 1) {

			/*
			 * what did spock play? (question after subject)
			 */
			answer += "Answer: " + subj.toUpperCase() + "  \t [" + pred
					+ "  \t " + obj.toUpperCase() + "] \t  ";

		} else if (questionType == 2) {

			/*
			 * what did spock play? (question after object)
			 */
			answer += "Answer: " + obj.toUpperCase() + "  \t [" + pred
					+ "  \t " + subj.toUpperCase() + "] \t  ";
		}

		else {

			/*
			 *   who played the lute?
			 */
			answer += "Answer: " + subj.toUpperCase() + "  \t " + pred
					+ "  \t " + obj.toUpperCase()+  " \t  ";

		}

		answer += "Context: ";

		for (String arg : args) {

			if (arg.equals(subj) || arg.equals(obj) || arg.equals(predLemma))
				continue;

			answer += arg + " , ";
		}

		return answer;
	}

	private void btnQueryActionPerformed(ActionEvent evt) {

		this.taResult.setText("");

		String predicate = tfPredicate.getText();
		String subject = tfArg1.getText();
		String object1 = tfArg2.getText();
		String object2 = tfArg3.getText();

		List<String> objects = new ArrayList<String>();

		if (!object1.trim().equals(""))
			objects.add(object1);

		if (!object2.trim().equals(""))
			objects.add(object2);

		if (subject.trim().equals(""))
			subject = null;

		String queryTranslation = "";

		/*
		 * subject to query translation
		 */
		if (subject == null)
			queryTranslation += "(who or what) ";
		else
			queryTranslation += subject + " ";

		if (predicate.trim().equals(""))
			queryTranslation += "(did what) ";
		else
			queryTranslation += predicate + " ";

		if (!object1.trim().equals(""))
			queryTranslation += object1 + " ";

		if (!object2.trim().equals(""))
			queryTranslation += object2 + " ";

		if (object1.trim().equals("") && object2.trim().equals("")) {
			queryTranslation += "(who or what) ";

		}

		queryTranslation += "?";

		this.taResult.append("Question: " + queryTranslation + "\n\n");

		List<String> responses = helper.query(predicate, subject, objects);

		this.taResult.append("Found " + responses.size() + " answers:  \n");

		if (subject == null)
			subject = "";

		/*
		 * iterate over responses
		 */
		for (String response : responses) {

			/*
			 * assume three cases. case 1: question after object
			 */
			if (!predicate.trim().equals("") && !subject.trim().equals("")
					&& object1.trim().equals("") && object2.trim().equals("")) {

				System.out.println("question after object");

				/*
				 * check if object exists
				 */
				String obj = helper.getRole(response, "obj");
				
				if (obj == "") {
					System.out.println(response + " skipped");
					continue;
				}

				String display = this.makeDisplayString(response, 2);

				this.taResult.append(display);

			} else
			/*
			 * case 2: question after subject
			 */
			if (!predicate.trim().equals("")
					&& subject.trim().equals("")
					&& (!object1.trim().equals("") || !object2.trim()
							.equals(""))) {

				String subj = helper.getRole(response, "sbj");

				if (subj == null) {
					System.out.println(response + " skipped");
					continue;
				}

				String display = this.makeDisplayString(response, 1);

				this.taResult.append(display);

			} else
			/*
			 * case 3: question after both subject and object
			 */
			if (!predicate.trim().equals("") && subject.trim().equals("")
					&& (object1.trim().equals("") && object2.trim().equals(""))) {

				System.out.println("question after subject");

				String subj = helper.getRole(response, "sbj");

				if (subj == null) {

					System.out.println(response + " skipped");
					continue;
				}

				String display = this.makeDisplayString(response, 3);

				this.taResult.append(display);

			} else {

				String display = this.makeDisplayString(response, 3);

				this.taResult.append(display);
			}
			this.taResult.append("\n\n");
		}

	}
}
